package com.item.model.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.item.model.entity.WxOrderDetail;
import com.item.model.entity.result.ResultVo;
import com.item.model.mapper.WxOrderDetailMapper;
import com.item.model.service.WxOrderDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.item.model.util.ResultVoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class WxOrderDetailServiceImpl extends ServiceImpl<WxOrderDetailMapper, WxOrderDetail> implements WxOrderDetailService {

    @Autowired
    private WxOrderDetailMapper detailMapper;

    @Transactional
    @Override
    public ResultVo addOrderDetail(String orderNo, JSONArray jsonArray) {
        Date createDate = new Date();
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject detail = jsonArray.getJSONObject(i);
            detail.put("create_time",createDate);
            detail.put("status",0);
            detail.put("order_no",orderNo);
            WxOrderDetail wxOrderDetail = JSONObject.toJavaObject(detail, WxOrderDetail.class);
            System.out.println("-------------"+wxOrderDetail);
            int num = detailMapper.insert(wxOrderDetail);
            if (num<1) return ResultVoUtil.error("订单明细保存失败");
        }
        return ResultVoUtil.success("订单生成");
    }
}
